
/**
 *
 * 求一数值中二进制中有多少个 1
 */
import java.util.Scanner;
public class Study15 {
// 算法三：通过n & (n-1) 的次数
    public static void main (String[] args) {
        Scanner scanner = new Scanner (System.in);
        int count = 0;
        if(scanner.hasNextInt()) {
            int num = scanner.nextInt();
            while(num !=0) {
                num = num & (num -1);
                count++;
            }
            System.out.println("该数二进制的 1 的个数是："+count);
        }
        scanner.close(); // 关闭 IO流，代码好习惯
    }



//    算法二:通过 移位运算符，再 & 运算判断
    public static void main2 (String[] args) {
        Scanner scanner = new Scanner (System.in);
        int count = 0; // 计数
        if(scanner.hasNextInt()) {
            int num = scanner.nextInt();
            for (int i = 0; i < 32; i++) {
                if( ((num >> i) & 1 ) == 1) {
                    count++;
                }
            }
            System.out.println("一共有"+count+"个 1");
        }
        scanner.close(); // 关闭 IO 流，代码好习惯；
    }




    // 算法一: 通过取模 % 运算就取出每一比特位数值，再判断
    public static void main1 (String[] args) {
        Scanner scanner = new Scanner (System.in);
        int count = 0; // 计数
        if(scanner.hasNextInt()) {
            int num = scanner.nextInt();
            while(num > 0) {
                if(num % 2 == 1) {
                    count++;
                }
                num /= 2;
            }
            System.out.println("一共有"+count+"个 1");
        }
        scanner.close(); // 关闭 IO流，代码好习惯
    }

}
